package org.java.net.socket;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class Client3 extends Frame{
	
	private TextArea msgView = new TextArea();
	private TextField sendBox = new TextField();
	private DataInputStream reader;
	private DataOutputStream writer;
	
	Socket socket;
	
	public Client3(String title)
	{
		super(title);
		msgView.setEditable(false);
		add(msgView,"Center");
		add(sendBox,"South");
		
		sendBox.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent ae)
					{
						try
						{
							writer.writeUTF(sendBox.getText());
							writer.flush();
							
							String fileInfo = reader.readUTF();
							msgView.setText("<" + sendBox.getText() 
									+ "的内容>\n\n");
							msgView.append(fileInfo);
							
							sendBox.setText("");
						}
						catch(Exception e)
						{}
					}
				}
		);
		pack();
	}
	
	private void connect()
	{
		try
		{
			msgView.append("尝试与服务器连接");
			socket = new Socket("127.0.0.1", 4747);
			msgView.append("连接完毕...请输入文件名\n");
			
			reader = new DataInputStream(socket.getInputStream());
			writer = new DataOutputStream(socket.getOutputStream());
		}
		catch(Exception e)
		{
			msgView.append("连接失败");
		}
	}
	
	public static void main(String[] args)
	{
		Client3 client = new Client3("查看服务器系统文件");
		client.setVisible(true);
		client.connect();
	}
}
